package com.test.daily.leetcode.y2022.m07.day0720.v03;

import java.util.PriorityQueue;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/7/20 下午 02:51
 * @version: 1.0
 */
public class Solution {
    public static int min1(int[] arr) {
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for (int i = 0; i < arr.length; i++) {
            pq.add(arr[i]);
        }
        int sum = 0;
        int cur = 0;
        while (pq.size() > 1) {
            cur = pq.poll() + pq.poll();
            sum += cur;
            pq.add(cur);
            cur = 0;
        }
        return sum;
    }

    public static int min2(int[] arr, int pre) {
        if (arr.length == 1) {
            return pre;
        }
        int sum = Integer.MAX_VALUE;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i; j < arr.length; j++) {
                sum = Math.min(sum, min2(merge(arr, i, j), pre + arr[i] + arr[j]));
            }
        }
        return sum;
    }

    public static int[] merge(int[] arr, int i, int j) {
        int[] help = new int[arr.length - 1];
        int l = 0;
        for (int k = 0; k < arr.length; k++) {
            if (k != i && k != j) {
                help[l++] = arr[k];
            }
        }
        help[l] = arr[i] + arr[j];
        return help;
    }
}
